package com.frank.libandroid.util.net;

import android.util.Log;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class RetrofitCallback<T> implements Callback<T> {
    private static final String TAG = "RetrofitCallback";

    public abstract void onResponse(T t);

    public void onFailure(int code, String msg) {
    }


    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if (response.isSuccessful()) {
            T body = response.body();
            onResponse(body);
        } else {
            onFailure(response.code(), response.message());
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        Log.e(TAG, "onFailure: ", t);
        onFailure(500, t.getMessage());
    }
}
